package com.douyang.handle;

import cn.dev33.satoken.oauth2.consts.GrantType;
import cn.dev33.satoken.oauth2.data.loader.SaOAuth2DataLoader;
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.douyang.base.expcetion.UserException;
import com.douyang.pojo.AuthClient;
import com.douyang.service.AuthClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SaTokenOauth2DateLoader implements SaOAuth2DataLoader {
    @Autowired
    private AuthClientService authClientService;


    @Override
    public SaClientModel getClientModel(String clientId) {
        AuthClient authClient = authClientService.getById(Long.valueOf(clientId));
        if (authClient == null) {throw new UserException("getClientModel:不存在");}
        return new SaClientModel()
                .setClientId(clientId)    // client id
//                .setClientSecret("aaaa-bbbb-cccc-dddd-eeee")    // client 秘钥
                .addAllowRedirectUris(authClient.getClientHostname())    // 所有允许授权的 url
//                .addContractScopes(JSONUtil.parseArray(authClient.getScopesJson()).toArray(new String[0]))    // 所有签约的权限
                .addAllowGrantTypes(     // 所有允许的授权模式
                        GrantType.authorization_code // 授权码式
//                        GrantType.implicit,  // 隐式式
//                        GrantType.refresh_token,  // 刷新令牌
//                        GrantType.password,  // 密码式
//                        GrantType.client_credentials  // 客户端模式
                );
    }
}
